/**
 * FileName: 新增编辑请求使用到的数据处理
 * Created Date: 2022-10-19 21:16:55
 * Author: 文初阳
 * -----
 * Last Modified: 2022-10-19 21:16:55
 * Modified By: 文初阳
 */

import moment from 'moment';

export function formatResumeData(data: any) {
    const {
        aboutMe,
        contactMe,
        knowMe,
        educationExperiences,
        personalSkills,
        projectExperiences,
        workExperiences,
        job,
        name,
        summary,
        resumeName,
    } = data;
    const aboutMeArr = aboutMe?.filter((ele: any) => ele && ele.value) || [];
    const contactMeArr = contactMe?.filter((ele: any) => ele && ele.label && ele.value) || [];
    const knowMeArr = knowMe?.filter((ele: any) => ele && ele.label && ele.value) || [];
    const baseInfo = {
        name: name || '王摸鱼',
        summary: summary || '',
        job: job || '总经理',
        aboutMe: aboutMeArr,
        contactMe: contactMeArr,
        knowMe: knowMeArr,
    };
    const personalSkillsArr = personalSkills?.filter((ele: any) => ele && ele.title && ele.content) || [];
    const workExperiencesArr =
        workExperiences
            ?.map((work: any) => {
                if (!work?.duty || !work?.date || !work?.company) return undefined;
                const workDutyArr = work.duty
                    ?.map((duty: any) => {
                        if (!duty?.detail) return undefined;
                        const dutyDetailArr = duty.detail?.filter((ele: any) => ele && ele.label && ele.value) || [];
                        if (dutyDetailArr.length === 0) {
                            return undefined;
                        }
                        return {
                            title: duty?.title || '',
                            subtitle: duty?.subtitle || '',
                            detail: dutyDetailArr,
                        };
                    })
                    .filter((ele: any) => ele !== undefined);
                if (workDutyArr.length === 0) return undefined;
                return {
                    company: work.company,
                    position: work.position || '',
                    startTime: moment(work.date?.[0]).format('YYYYMM'),
                    endTime: moment(work.date?.[0]).format('YYYYMM'),
                    duty: workDutyArr,
                };
            })
            ?.filter((ele: any) => ele !== undefined) || [];
    const projectExperiencesArr =
        projectExperiences
            ?.map((proj: any) => {
                if (!proj?.title || !proj?.detail) return undefined;
                const projDetailArr = proj.detail.filter((ele: any) => ele && ele?.label && ele?.value) || [];
                if (projDetailArr.length === 0) return undefined;
                return {
                    title: proj.title,
                    summary: proj?.summary || '',
                    image: proj?.image || '',
                    detail: projDetailArr,
                };
            })
            ?.filter((ele: any) => ele !== undefined) || [];
    const educationExperiencesArr =
        educationExperiences
            ?.map((edu: any) => {
                if (!edu?.date || !edu?.major || !edu?.school || !edu?.content) return undefined;
                const contentArr =
                    edu.content?.filter((ele: any) => ele && ele?.value).map((ele: { value: string }) => ele.value) ||
                    [];
                if (contentArr.length === 0) return undefined;
                return {
                    startTime: moment(edu.date?.[0]).format('YYYYMM'),
                    endTime: moment(edu.date?.[0]).format('YYYYMM'),
                    major: edu.major,
                    school: edu.school,
                    content: contentArr,
                };
            })
            ?.filter((ele: any) => ele !== undefined) || [];
    return {
        name: resumeName,
        content: {
            baseInfo: [baseInfo],
            personalSkills: personalSkillsArr,
            workExperiences: workExperiencesArr,
            projectExperiences: projectExperiencesArr,
            educationExperiences: educationExperiencesArr,
        },
    };
}
